package com.geline.sqlbackup.util;

import cn.hutool.core.compiler.JavaSourceCompiler;
import cn.hutool.core.io.resource.StringResource;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.ClassUtil;

/**
 * Java实现反射单、多个参数方法Demo https://blog.csdn.net/l546492845/article/details/126542032
 * 关于Java动态编程的简单总结 https://blog.csdn.net/star_sky_red/article/details/54586145
 * @author: mengxin
 * @date: 2022/11/16 18:00
 */
public class JavaClassUtil {

    //解析java源代码编译加载到内存中
    public static Class<?> addClass(String fullClassName, String srcCode) throws ClassNotFoundException {
        ClassLoader classLoader = JavaSourceCompiler.create(ClassLoaderUtil.getClassLoader())
                .addSource(new StringResource(srcCode, fullClassName))
                .compile();
        return classLoader.loadClass(fullClassName);
    }

    /**
     * 获取Class
     * @param fullClassName
     * @return
     */
    public static Class getClass(String fullClassName) {
        return ClassUtil.loadClass(fullClassName);
    }

}
